package com.qin.entity;

import com.qin.service.ExecuteService;
import com.qin.service.FunctionService;
import com.qin.service.impl.ExecuteServiceImpl;
import com.qin.service.impl.FunctionServiceImpl;
import lombok.Data;
import java.util.List;

@Data
public class Function {

    //主键
    private String function_id;
    //函数名
    private String function_name;
    //所属系统
    private String system_name;
    //函数说明
    private String function_descption;
    //创建人
    private String user_created;
    //函数入参
    private String input_parameters;
    //参数说明
    private String parameter_descption;

    //参数说明
    private List<TestCase> testCaseList;

    private ExecuteService executeService = new ExecuteServiceImpl();

    public void execute(ExecuteBean executeBean){
        //补全本Function对象的各属性
        this.complete();
        //设置ExecuteBean对象的各属性
        executeBean.setFunction_id(function_id);
        //设置ExecuteLog对象的各属性
        ExecuteLog executeLog = new ExecuteLog(executeBean.getExecute_id(), executeBean.getFunction_id(),
                "", "", "", "");
        //插入日志
        executeService.insertExecuteLog(executeLog, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>公共函数【" + function_name + "】执行开始");
        System.out.println("【executeBean】"+executeBean);

        //获取实参
        String[] input_paravalue = null;
        if(executeBean.getInput_paravalue() != null && !"".equals(executeBean.getInput_paravalue().trim())){
            input_paravalue = executeBean.getInput_paravalue().split(",");
            //去空格
            for(int i=0; i<input_paravalue.length; i++){
                input_paravalue[i] = input_paravalue[i].trim();
            }
        }

        //获取形参
        String[] input_paraname = null;
        if(input_parameters.trim() != null && !"".equals(input_parameters.trim())){
            input_paraname = input_parameters.split(",");
            //去空格
            for(int i=0; i<input_paraname.length; i++){
                input_paraname[i] = input_paraname[i].trim();
            }
        }

        //对比形参和实参的参数个数
        if(input_paravalue != null && input_paraname != null && input_paravalue.length == input_paraname.length){
            for(int i=0; i<input_paraname.length; i++){
                //插入日志
                executeService.insertExecuteLog(executeLog, "【函数入参】变量名=" + input_paraname[i] + "， 变量值=" + input_paravalue[i]);

                //把queryResultList添加到executeBean的dbQueryResultMap中
                executeBean.getParaMap().put(input_paraname[i], input_paravalue[i]);
            }
        }
        else{
            //插入日志
            executeService.insertExecuteLog(executeLog, "输入的参数个数和函数定义的入参个数不一致");
            throw new NullPointerException();
        }


        //执行组件的execute方法
        if(testCaseList != null){
            for (int i=0; i<testCaseList.size(); i++){
                testCaseList.get(i).execute(executeBean);
            }
        }

        //插入日志
        executeService.insertExecuteLog(executeLog, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>公共函数【" + function_name + "】执行结束");

        System.out.println("【executeBean.getParaMap()】" + executeBean.getParaMap());
        System.out.println("【executeBean.getDbQueryResultMap()】" + executeBean.getDbQueryResultMap());
        System.out.println("【executeBean.getInterfaceResultMap()】" + executeBean.getInterfaceResultMap());

    }

    public void complete(){
        //补全本Function对象的各属性
        FunctionService functionService = new FunctionServiceImpl();
        Function function = functionService.queryFunctionByFunctionId(this);

        this.setFunction_id(function.function_id);
        this.setFunction_name(function.function_name);
        this.setSystem_name(function.system_name);
        this.setFunction_descption(function.function_descption);
        this.setUser_created(function.user_created);
        this.setInput_parameters(function.input_parameters);
        this.setParameter_descption(function.function_descption);

        this.setTestCaseList(function.testCaseList);
    }

}
